package com.blog.controller;

import com.blog.entity.Type;
import com.blog.service.BlogService;
import com.blog.service.TypeService;
import com.blog.vo.FirstPageBlog;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 分类页面显示控制器
 */
@Controller
public class TypeShowController {

    @Autowired
    private TypeService typeService;

    @Autowired
    private BlogService blogService;

    //分页查询分类
    @GetMapping("/types/{id}")
    public String types(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, @PathVariable Long id, Model model) {
        List<Type> types = typeService.getAllTypeAndBlog();
        //-1表示从首页导航点进来的
        if (id == -1) {
            id = types.get(0).getId();
        }

        PageHelper.startPage(pageNum, 8);
        List<FirstPageBlog> blogs = blogService.getByTypeId(id);

//        PageInfo<FirstPageBlog> blogs = new PageInfo<>(blog);

        model.addAttribute("types", types);
        model.addAttribute("blogs", blogs);
        model.addAttribute("activeTypeId", id);

//        System.out.println("==+++++++++++++++=="+types);
//        System.out.println("==================="+blogs);
//        System.out.println("==---------------=="+id);

        return "types";
    }

}